<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <p>
        账号: <input type="text" class="user">
    </p>
    <p>
        密码: <input type="text" class="pwd">
    </p>
    <p>
        <button class="btn">登录</button>
    </p>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.27.2/axios.js"></script>
<script>
    let user = document.querySelector('.user');
    let pwd = document.querySelector('.pwd');
    let btn = document.querySelector('.btn');

    axios.interceptors.request.use((config)=>{
        config.headers.token = sessionStorage.getItem("token")?sessionStorage.getItem("token"):""
        return config
    })

    btn.onclick = function(){
        axios.post("/register",{
            userName:user.value,
            password:pwd.value
        }).then(res=>{
            //登陆后对token值进行存储
            //如果后端有反馈的token值证明信息正确
            if(res.data.token){
                sessionStorage.setItem("token",res.data.token)
                alert(res.data.message)
                location.href = "./home.html"
            //不正确则弹出错误信息
            }else{
                alert(res.data.message)
            }   
        })
    }
</script>
</html>